package com.breaktian.assemble.image;

import java.io.File;


import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.breaktian.assemble.utils.LogUtil;


/**
 * Created by breaktian on 2016/6/21.
 * 图片加载统一入口
 *
 * eclipse formater
 */
public class ImageUtils {

    private String tag = getClass().getSimpleName();

    private final static Object lockObj = new Object();

    private static ImageUtils instance;
    private BaseImageLoaderProvider mProvider;

    //占位图
//    public int placeholderId = R.mipmap.ic_launcher;

    /**
     * 显示方式
     * */
    public enum DisplayType{
		CENTERCROP, // 可能会完全填充ImageView，但图像可能不会完整显示。
        FITCENTER//图像将会完全显示，但可能不会填满整个 ImageView
    }


    private ImageUtils(){
        //默认使用glide
        mProvider = new GlideImageLoaderProvider();
    }


    public static ImageUtils get(){
        if(instance==null){
            synchronized (lockObj){
                if(instance==null){
                    instance = new ImageUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 设置provider
     * */
    public void setImageLoaderProvider(BaseImageLoaderProvider provider){
        this.mProvider = provider;
    }

    public void loadGif(Context context, String url, ImageView imageView, int error){
        if(mProvider instanceof GlideImageLoaderProvider){
            ((GlideImageLoaderProvider)mProvider).loadGif(context,url,imageView,error);
        }else{
//            throw new UnsupportedOperationException("gif图片加载失败：provider暂不支持此方法");
			LogUtil.e("gif图片加载失败：provider暂不支持此方法");
        }
    }

	public void loadGif(Context context, int resid, ImageView imageView) {
		if (mProvider instanceof GlideImageLoaderProvider) {
			((GlideImageLoaderProvider) mProvider).loadGif(context, resid, imageView);
		} else {
			// throw new UnsupportedOperationException("gif图片加载失败：provider暂不支持此方法");
			LogUtil.e("gif图片加载失败：provider暂不支持此方法");
		}
	}

	public void loadVideo(Context context, String url, ImageView imageView, int placeholder,
                          int error) {
		if (mProvider instanceof GlideImageLoaderProvider) {
			((GlideImageLoaderProvider) mProvider).loadVideo(context, url, imageView, placeholder,
					error);
		} else {
			// throw new UnsupportedOperationException("gif图片加载失败：provider暂不支持此方法");
			LogUtil.e("gif图片加载失败：provider暂不支持此方法");
		}
	}


    public void loadImage(Context context, String url, ImageView imageView) {
        mProvider.loadImage(context,url,imageView);
    }

    public void loadImage(Context context, String url, ImageView imageView, int placeholder) {
        mProvider.loadImage(context,url,imageView,placeholder);
    }

    public void loadImage(Context context, String url, ImageView imageView, DisplayType displaytype) {
        mProvider.loadImage(context,url,imageView,displaytype);
    }

    public void loadImage(Context context, String url, ImageView imageView, int placeholder, DisplayType displaytype) {
        mProvider.loadImage(context,url,imageView,placeholder,displaytype);
    }

    public void loadResizeImage(Context context, String url, ImageView imageView, int width, int height) {
        mProvider.loadResizeImage(context,url,imageView,width,height);
    }

    public void loadResizeImage(Context context, String url, ImageView imageView, int placeholder, int width, int height) {
        mProvider.loadResizeImage(context,url,imageView,placeholder,width,height);
    }

    public void loadRoundImage(Context context, String url, ImageView imageView) {
        mProvider.loadRoundImage(context,url,imageView);
    }

	public void loadRoundImage(Context context, int resid, ImageView imageView) {
		mProvider.loadRoundImage(context, resid, imageView);
	}

    public void loadImage(Context context, File file, ImageView imageView) {
        mProvider.loadImage(context,file,imageView);
    }


    public void loadImage(Context context, Uri uri, ImageView imageView) {
        mProvider.loadImage(context,uri,imageView);
    }


    public void loadImage(Context context, int recourceId, ImageView imageView) {
        mProvider.loadImage(context,recourceId,imageView);
    }







}
